package com.ama.controller;

import com.ama.pojo.Company;
import com.ama.pojo.Person;
import com.ama.service.CompanyService;
import com.ama.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class LoginController {
    @Autowired
    PersonService personService;
    @Autowired
    CompanyService companyService;
    @RequestMapping("/loginConfirm")
    public void loginConfirm(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remember = request.getParameter("rem");
        if(remember!=null){
            Cookie c1 = new Cookie("username",username);
            Cookie c2 = new Cookie("password",password);
            c1.setMaxAge(60*60*24*365);
            c2.setMaxAge(60*60*24*365);
            response.addCookie(c1);
            response.addCookie(c2);
        }
        String type = request.getParameter("type");
        HttpSession session = request.getSession();
        if("个人".equals(type)){
            Person person = personService.login(username, password);
            if(person == null){
                response.sendRedirect("login.jsp");
            }else {
                session.setAttribute("person",person);
                response.sendRedirect("index.jsp");
            }
        }
        if("公司".equals(type)){
            Company company = companyService.login(username, password);
            if(company == null){
                response.sendRedirect("login.jsp");
            }else {
                session.setAttribute("company",company);
                response.sendRedirect("c_index.jsp");
            }
        }
    }
}
